<template>
	<view class="page">
		<view class="form-container">
			<!-- 表单项 -->
			<view class="form-item">
				<view class="form-label">客户姓名：</view>
				<input type="text" class="form-input" placeholder="请输入客户姓名" v-model="formData.name" />
			</view>
			
			<view class="form-item">
				<view class="form-label">客户手机号：</view>
				<input type="number" class="form-input" placeholder="请输入客户联系方式" v-model="formData.phone" maxlength="11" />
			</view>
			
			<view class="form-item">
				<view class="form-label">机器序列号：</view>
				<view class="serial-input-container">
					<input type="text" class="form-input serial-input" placeholder="请扫描输入序列号" v-model="formData.serialNumber" />
					<view class="scan-icon" @click="scanCode">
						<image src="/static/img/scan-icon.png" mode="aspectFit"></image>
					</view>
				</view>
			</view>
			
			<view class="form-item">
				<view class="form-label">订单号验证：</view>
				<input type="number" class="form-input" placeholder="请输入订单号后4位" v-model="formData.orderNumber" maxlength="4" @input="checkOrderNumber" />
			</view>
			
			<!-- 提交按钮 -->
			<view class="submit-btn" @click="submitForm">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	const api = require('../../config/api');
	const util = require('../../utils/util');
	
	export default {
		data() {
			return {
				formData: {
					name: '',
					phone: '',
					serialNumber: '',
					orderNumber: ''
				}
			}
		},
		methods: {
			// 扫码功能
			scanCode() {
				uni.scanCode({
					success: (res) => {
						this.formData.serialNumber = res.result;
					},
					fail: () => {
						uni.showToast({
							title: '扫码失败',
							icon: 'none'
						});
					}
				});
			},
			
			// 检查订单号输入
			checkOrderNumber(e) {
				// 限制只能输入4位数字
				let value = this.formData.orderNumber;
				if (value.length > 4) {
					this.formData.orderNumber = value.slice(0, 4);
				}
			},
			
			// 验证手机号格式
			validatePhone(phone) {
				const phoneReg = /^1[3-9]\d{9}$/;
				return phoneReg.test(phone);
			},
			
			// 提交表单
			submitForm() {
				// 表单验证
				if (!this.formData.name) {
					return uni.showToast({
						title: '请输入客户姓名',
						icon: 'none'
					});
				}
				
				if (!this.formData.phone) {
					return uni.showToast({
						title: '请输入客户手机号',
						icon: 'none'
					});
				}
				
				// 验证手机号格式
				if (!this.validatePhone(this.formData.phone)) {
					return uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					});
				}
				
				if (!this.formData.serialNumber) {
					return uni.showToast({
						title: '请输入机器序列号',
						icon: 'none'
					});
				}
				
				// if (!this.formData.orderNumber) {
				// 	return uni.showToast({
				// 		title: '请输入订单号后4位',
				// 		icon: 'none'
				// 	});
				// }
				
				// // 验证订单号是否为4位
				// if (this.formData.orderNumber.length !== 4) {
				// 	return uni.showToast({
				// 		title: '订单号必须为4位',
				// 		icon: 'none'
				// 	});
				// }
				
				// 提交表单逻辑
				uni.showLoading({
					title: '提交中...'
				});
				
				// 构造请求参数
				const requestData = {
					name: this.formData.name,
					phone: this.formData.phone,
					sn: this.formData.serialNumber
					,orderNo: this.formData.orderNumber
				};
				
				// 构造请求头
				const headers = {
					'Content-Type': 'application/json',
					'token': uni.getStorageSync('token'),
				};
				
				// 发送请求
				uni.request({
					url: api.merchantDjAddUrl,
					method: 'POST',
					data: requestData,
					header: headers,
					success: (res) => {
						uni.hideLoading();
						
						if (res.data.code === 0) {
							// 成功
							uni.showToast({
								title: '提交成功',
								icon: 'success'
							});
							
							// 返回上一页
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						} else {
							// 失败
							uni.showToast({
								title: res.data.msg || '提交失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.hideLoading();
						console.error(err);
						uni.showToast({
							title: '网络异常，请稍后重试',
							icon: 'none'
						});
					}
				});
			},
			
			// 重置表单
			resetForm() {
				this.formData = {
					name: '',
					phone: '',
					serialNumber: '',
					orderNumber: ''
				};
			}
		}
	}
</script>

<style lang="scss">
	.page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;
	}
	
	.form-container {
		background-color: #ffffff;
		border-radius: 10rpx;
		padding: 30rpx;
	}
	
	.form-item {
		margin-bottom: 30rpx;
		display: flex;
		align-items: center;
	}
	
	.form-label {
		font-size: 28rpx;
		color: #666;
		width: 180rpx;
		flex-shrink: 0;
	}
	
	.form-input {
		flex: 1;
		height: 90rpx;
		border-bottom: 1px solid #eee;
		font-size: 28rpx;
		color: #333;
	}
	
	.serial-input-container {
		display: flex;
		align-items: center;
		border-bottom: 1px solid #eee;
		flex: 1;
	}
	
	.serial-input {
		flex: 1;
		border-bottom: none;
	}
	
	.scan-icon {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.scan-icon image {
		width: 40rpx;
		height: 40rpx;
	}
	
	.submit-btn {
		height: 90rpx;
		background-color: #007AFF;
		color: #ffffff;
		font-size: 32rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 45rpx;
		margin-top: 60rpx;
	}
</style> 